package org.mm.proxycache;

import org.apache.mina.common.IoFilterAdapter;
import org.apache.mina.common.IoSession;

public class ProxyClientHttpsFilterBypass extends IoFilterAdapter
{

    /*
     * (non-Javadoc)
     * 
     * @see org.apache.mina.common.IoFilterAdapter#messageReceived(org.apache.mina.common.IoFilter.NextFilter,
     *      org.apache.mina.common.IoSession, java.lang.Object)
     */
    @Override
    public void messageReceived( NextFilter nextFilter, IoSession session, Object message )
            throws Exception
    {
        if ( session.getAttribute( ProxyClientHttpsFilter.HttpsMarker ) == null )
        {
            nextFilter.messageReceived( session, message );
            return;
        }

        // If message is raw SSL data, just forward it to server/client
        IoSession pairConnection = (IoSession) session
                .getAttribute( ProxyClientHttpsFilter.HttpsPairConnection );

        pairConnection.write( message );
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.apache.mina.common.IoFilterAdapter#sessionClosed(org.apache.mina.common.IoFilter.NextFilter,
     *      org.apache.mina.common.IoSession)
     */
    @Override
    public void sessionClosed( NextFilter nextFilter, IoSession session ) throws Exception
    {
        if ( session.getAttribute( ProxyClientHttpsFilter.HttpsMarker ) == null )
        {
            nextFilter.sessionClosed( session );
            return;
        }

        IoSession pairConnection = (IoSession) session
                .getAttribute( ProxyClientHttpsFilter.HttpsPairConnection );
        pairConnection.close();
    }

}
